
<div ng-controller="Customer_AddC">
    <div class="modal fade" id="dlg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">客户选择</h4>
                </div>
                <div class="modal-body">
                    <div class="input-group ">
                        <input class="selectFind form-control" type="text" ng-model="searchTxt" placeholder="查找" ng-change="FindAllBy()">
                        <span class="glyphicon glyphicon-remove input-group-addon" ng-click="empty()"></span>
                    </div>
                    <div class="selectShow" style=" width: 100%; height: 200px;border:1px solid #CCCCCC;overflow:scroll;">
                        <div style="cursor:pointer;" class="form-control" ng-click="selectEmp(x)" ng-repeat="x in client | filter:{ShortName : searchTxt}">{{x.ShortName}}</div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">添加客户</h1>
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <span class="h4">添加客户</span>
                            <span class="input-group-addon" style="float: right; width: 100px;" ng-click="FindAllBy()">查询</span>
                        </div>
                        <div class="panel-body">
                            <div class="form-group">
                                <label>简称 <em class="text-muted">(2-15字符)(必填)</em></label>
                                <input type="text" class="form-control" ng-model="data.ShortName" required minlength="2" maxlength="15">
                            </div>
                            <div class="form-group">
                                <label>名称(必填)</label>
                                <input type="text" required class="form-control" ng-model="data.Name">
                            </div>
                            <div class="form-group">
                                <label style="margin-right: 10px;">是否个人账户</label>
                                <label class="i-switch bg-info">
                                    <input type="checkbox" ng-model="data.IsPersonal">
                                    <i></i>
                                </label>
                            </div>
                            <div class="form-group">
                                <label>地区 </label>
                                <div class="input-group ">
                                    <span class="input-group-addon">省</span>
                                    <select class="form-control" ng-options="x.FillName for x in districts" ng-model="selectedDistrict1"></select>
                                    <span class="input-group-addon">市</span>
                                    <select class="form-control" ng-options="y as y.Name for y in selectedDistrict1.Children" ng-model="data.District"></select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>地址</label>
                                <input type="text" class="form-control" ng-model="data.Address">
                            </div>

                            <!--------------------动态加载联系人行 Start--------------------------------------------------->

                            <div class="form-group">
                                <div id="Contacts-wraper">
                                    <button type="button" class="btn btn-default" ng-click=" AddContact()">新增</button>
                                    <div class="Contacts-inner" ng-repeat="x in data.CustomerContacts">
                                        <label>联系人{{$index + 1}}</label>
                                        <input type="text" ng-model="x.ContactUser">
                                        <label>电话(必填)</label>
                                        <input type="text" ng-model="x.ContactPhone" required ng-pattern="/^[1][3-8]\d{9}$/">

                                        <button type="button" class="btn btn-default" ng-click="remove($index)">删除</button>
                                    </div>

                                </div>
                            </div>

                      

                            <div class="form-group">
                                <div id="Contacts-wraper">
                                    <button type="button" class="btn btn-default" ng-click=" AddAccountantContact()">新增</button>
                                    <div class="Contacts-inner" ng-repeat="x in data.AccountantContacts">
                                        <label>财务{{$index + 1}}</label>
                                        <input type="text" ng-model="x.ContactUser">
                                        <label>财务电话</label>
                                        <input type="text" ng-model="x.ContactPhone" ng-pattern="/^[1][3-8]\d{9}$/">

                                        <button type="button" class="btn btn-default" ng-click="removeAccountant($index)">删除</button>
                                    </div>

                                </div>
                            </div>
                            <!--------------------End--------------------------------------------------->

                            <!--<div class="form-group">
                                <label>财务</label>
                                <input type="text" class="form-control" ng-model="data.Accountant">
                            </div>
                            <div class="form-group">
                                <label>财务电话</label>
                                <input type="text" class="form-control" ng-model="data.AccountantPhone" ng-pattern="/^[1][3-8]\d{9}$/">
                            </div>-->
                            <div class="form-group">
                                <label>客户专员(必填)</label>
                                <!--<select ui-jq="chosen" required class="form-control" ng-options="x as x.Name for x in userdata" ng-model="data.ClientManager"></select>-->
                                <button class="form-control text-left" ng-model="data.ClientManager.Name" type="text" data-toggle="modal" data-target="#dlgClient">
                                    {{data.ClientManager.Name}}
                                </button>
                            </div>
                            <div class="form-group" ng-if="data.IsPersonal==true">
                                <label>身份证号</label>
                                <input type="text" class="form-control" ng-pattern="/^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/" ng-model="data.IDCode">
                            </div>
                            <div class="form-group" ng-if="data.IsPersonal==false">
                                <label>信用代码</label>
                                <input type="text" class="form-control" ng-model="data.CreditID">
                            </div>
                            <div class="form-group">
                                <label>备注</label>
                                <input type="text" class="form-control" ng-model="data.Remarks">
                            </div>
                        </div>
                        <footer class="panel-footer text-right bg-light lter" style="text-align: center">
                            <button type="button" class="btn btn-success" ng-click="edit()" ng-disabled="form.$invalid || !data.ClientManager">提交</button>
                            <span style="padding: 0 5%"></span>
                            <button type="button" class="btn btn-default" ng-click="$state.go('app.Customer_List')">取消</button>
                        </footer>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="modal fade" id="dlgClient" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">人员选择</h4>
                </div>
                <div class="modal-body">
                    <!--<input class="selectFind form-control" type="text" ng-model="searchTxt" placeholder="查找"/>-->
                    <div class="input-group ">
                        <input class="selectFind form-control" type="search" ng-model="searchClientTxt" placeholder="查找">
                        <span class="glyphicon glyphicon-remove input-group-addon" ng-click="emptyClient()"></span>
                    </div>
                    <div class="selectShow" style="width: 100%; height: 200px; border: 1px solid #CCCCCC; overflow: auto;">
                        <div style="cursor: pointer;" class="form-control" ng-click="selectClientEmp(x)" ng-repeat=" x in userdata|filter:{Name : searchClientTxt}">{{x.Name}}</div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>

                </div>
            </div>
        </div>
    </div>
</div>
<!-- Button trigger modal -->
<!--<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
    Launch demo modal
</button>-->



<script>

    //$('#dlg').modal({
    //    keyboard: false
    //});
    angular.module('app') //非标优惠
        .controller("Customer_AddC", function ($scope, $state, $http, $stateParams) {
            $scope.data = {
                ApprovalState: 2
            }; $scope.searchTxt = "";
            $scope.FindAllBy = function () {
                $('#dlg').modal("show");
                $http({
                    url: "../api/ClientManager/GetCustomer?name=" + $scope.searchTxt,
                    method: "GET"
                })
               .then(function (res) {
                   if (res.State <= 0) {
                       alert(res.Msg);
                   } else {
                       $scope.client = res.data;
                   }
               })
               .then(function (error) {
                   if (error) {
                       console.log("error");
                   }
               });
            }

            //----------------------------动态加载联系人行  Start---------------------------------------------------------------------------------------------------------------------------------

            $scope.data.CustomerContacts = [{ ContactType: 0, ContactUser: "", ContactPhone: "" }];
            $scope.data.AccountantContacts = [{ ContactType: 1, ContactUser: "", ContactPhone: "" }];
            
            //新增联系人行
            $scope.AddContact = function () {

                $scope.obj = { ContactType: 0, ContactUser: "", ContactPhone: "" };
                $scope.data.CustomerContacts.push($scope.obj);
            };
            //新增财务联系人行
            $scope.AddAccountantContact = function () {

                $scope.obj = { ContactType: 1, ContactUser: "", ContactPhone: "" };
                $scope.data.AccountantContacts.push($scope.obj);
            };
            //删除联系人行
            $scope.remove = function (index) {
               
                if ($scope.data.CustomerContacts.length > 1) {
                    $scope.data.CustomerContacts.splice(index, 1);
                } else {
                    alert("最少要留有一个联系方式");
                }
            }

            $scope.removeAccountant= function (index) {
               
                if ($scope.data.AccountantContacts.length >0) {
                    $scope.data.AccountantContacts.splice(index, 1);
                } else {
                    alert("已经没有可删除的行");
                }
            }
            //---------------------------End-----------------------------------------------------------------------------------------------------------------------------

            //获取地市
            $http({
                url: "/api/District",
                method: "GET"
            })
            .then(function (res) {
                $scope.districts = res.data;

            })
            .then(function (error) {
                if (error) {
                    console.log("error");
                }
            });
            $scope.edit = function () {
                //angular.forEach($scope.data.AccountantContacts, function (data) {
                //    $scope.data.CustomerContacts.push(data);
                //});
               
                $http.post("/api/Customer/AddCustomer", $scope.data)
                .then(function (res) {
                    //身份证号码验证
                    $("#person_id").change(function () {
                        if ($("#person_id").val() != "") {
                            //身份证的地区代码对照
                            var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" };
                            //获取证件号码
                            var person_id = $("#person_id").val();
                            //合法性验证
                            var sum = 0;
                            //出生日期
                            var birthday;
                            //验证长度与格式规范性的正则
                            var pattern = new RegExp(/(^\d{15}$)|(^\d{17}(\d|x|X)$)/i);
                            if (pattern.exec(person_id)) {
                                //验证身份证的合法性的正则
                                pattern = new RegExp(/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/);
                                if (pattern.exec(person_id)) {
                                    //获取15位证件号中的出生日期并转位正常日期
                                    birthday = "19" + person_id.substring(6, 8) + "-" + person_id.substring(8, 10) + "-" + person_id.substring(10, 12);
                                }
                                else {
                                    person_id = person_id.replace(/x|X$/i, "a");
                                    //获取18位证件号中的出生日期
                                    birthday = person_id.substring(6, 10) + "-" + person_id.substring(10, 12) + "-" + person_id.substring(12, 14);

                                    //校验18位身份证号码的合法性
                                    for (var i = 17; i >= 0; i--) {
                                        sum += (Math.pow(2, i) % 11) * parseInt(person_id.charAt(17 - i), 11);
                                    }
                                    if (sum % 11 != 1) {
                                        $(this).addClass("txtRequired");
                                        alert("身份证号码不符合国定标准，请核对！");
                                        //                                $("#person_id").val("");
                                        //                                $("#person_id").css("border-color","red")
                                        return form.$invalid = true;
                                    }
                                }
                                //检测证件地区的合法性
                                if (aCity[parseInt(person_id.substring(0, 2))] == null) {
                                    $(this).addClass("txtRequired");
                                    alert("证件地区未知，请核对！");
                                    return form.$invalid = true;
                                }
                                var dateStr = new Date(birthday.replace(/-/g, "/"));

                                //alert(birthday +":"+(dateStr.getFullYear()+"-"+ Append_zore(dateStr.getMonth()+1)+"-"+ Append_zore(dateStr.getDate())))
                                if (birthday != (dateStr.getFullYear() + "-" + Append_zore(dateStr.getMonth() + 1) + "-" + Append_zore(dateStr.getDate()))) {
                                    $(this).addClass("txtRequired");
                                    alert("证件出生日期非法！");
                                    //                            $("#birthday").val("");
                                    return form.$invalid = true;
                                }
                                $(this).removeClass("txtRequired");
                                //                        $("#birthday").val(birthday);
                            }
                            else {
                                $(this).addClass("txtRequired");
                                alert("证件号码格式非法！");
                                //$("#person_id").val("");
                                return form.$invalid = true;
                            }

                        } else {
                            alert("请输入证件号！");
                            $("#birthday").val("");
                            return form.$invalid = true;
                        }
                        return form.$invalid = false;
                    });
                    if (form.$invalid = true) {
                        alert("保存成功");
                        $state.go('app.Customer_List');
                    } else {
                        alert("保存失败，请联系你的管理员");
                    }
                });
            };
            //获取人员
            $http({
                url: "../api/Employee/List?name=&page=1&rows=9999",
                method: "GET"
            })
            .then(function (res) {
                $scope.userdata = res.data.Data;
            })
            .then(function (error) {
                if (error) {
                    console.log("error");
                }
            });
            $scope.selectEmp = function (emp) {
                $('#dlg').modal("hide");
            };
            $scope.selectClientEmp = function (emp) {
                $scope.data.ClientManager = emp;
                $('#dlgClient').modal("hide");
            };
            $scope.empty = function () {
                $scope.searchTxt = "";
            };
            $scope.emptyClient = function () {
                $scope.searchClientTxt = "";
            };
        });

</script>